package com.css.devops.rest;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.css.devops.domain.View;
import com.css.devops.service.DevopsView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "Devops：操作View视图的各种接口")
@RestController
@RequestMapping("${devops-context}/view")
public class DevopsViewResource {

	@Resource(name = "${devops.service.view}")
	private DevopsView devopsView;
	
	@ApiOperation(value = "创建视图")
    @PostMapping
    public ResponseEntity<String> createView(@RequestBody View view){
		try {
			devopsView.createView(view);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("视图创建失败!");
		}
		return ResponseEntity.ok("视图创建成功!");
    }
	
	@ApiOperation(value = "获取视图详情")
    @GetMapping("{viewName}")
    public ResponseEntity<View> getView(@PathVariable String viewName){
		View view = null;
		try {
			view = devopsView.getView(viewName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(view);
		}
		return ResponseEntity.ok(view);
    }
	
	@ApiOperation(value = "获取视图配置")
    @GetMapping("getViewConfig/{viewName}")
    public ResponseEntity<String> getViewConfig(@PathVariable String viewName){
		String viewConfig = null;
		try {
			viewConfig = devopsView.getViewConfig(viewName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(viewConfig);
		}
		return ResponseEntity.ok(viewConfig);
    }
	
	@ApiOperation(value = "修改视图")
	@PutMapping
    public ResponseEntity<String> updateView(@RequestBody View view){
		try {
			devopsView.updateView(view);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("视图修改失败!");
		}
		return ResponseEntity.ok("视图修改成功!");
    }
	
	@ApiOperation(value = "删除视图")
	@DeleteMapping("{viewName}")
    public ResponseEntity<String> deleteView(@PathVariable String viewName){
		try {
			devopsView.deleteView(viewName);
		}catch (Exception e) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body("视图删除失败!");
		}
		return ResponseEntity.ok("视图删除成功!");
    }
}
